package com.wonders.xianningsocialinsurance.adapters;

import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.net.Uri;
import android.support.design.widget.Snackbar;
import android.support.v7.widget.CardView;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.baidu.mapapi.search.core.PoiInfo;
import com.baidu.mapapi.utils.DistanceUtil;
import com.wonders.xianningsocialinsurance.App;
import com.wonders.xianningsocialinsurance.R;

import java.net.URISyntaxException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by Woodlin on 2016/7/14.
 */

public class PoiSearchResultAdapter extends RecyclerView.Adapter  {
    private List<PoiInfo> poiInfoList;
        private Context mContext;
        public PoiSearchResultAdapter(Context context, List<PoiInfo> objects) {
            poiInfoList = objects;
            mContext = context;
        }
    @Override
        public PhotoViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
            View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.search_recycler_item, null);
                 LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
                  view.setLayoutParams(lp);
            return new PhotoViewHolder(view);
        }

        @Override
        public void onBindViewHolder(final RecyclerView.ViewHolder viewHolder, final int position) {
            PhotoViewHolder holder = (PhotoViewHolder)viewHolder;
            holder.textView_address.setText(poiInfoList.get(position).address);
            holder.textView_name.setText(poiInfoList.get(position).name);
            String distance= DistanceUtil.getDistance(App.location_latlng, poiInfoList.get(position).location)+"";
            distance=distance.substring(0, distance.indexOf("."));
            holder.distance.setText("/"+distance+"米");
            holder.cardView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if (isAvilible(mContext,"com.baidu.BaiduMap")){
                        try {

                            Intent intent = Intent
                                    .getIntent("bdapp://map/direction?origin=latlng:"
                                            + App.location_latlng.latitude
                                            + ","
                                            + App.location_latlng.longitude
//
                                            + "|name:我的位置&destination=latlng:"
                                            + poiInfoList.get(position).location.latitude
                                            + ","
                                            + poiInfoList.get(position).location.longitude
                                            + "|name:"
                                            + poiInfoList.get(position).name
                                            + "&mode=transit&region=青岛&src=移动医疗护理#Intent;scheme=bdapp;package=com.baidu.BaiduMap;end");
                            mContext.startActivity(intent);
                            Log.e("position",poiInfoList.get(position).location.latitude+"--"+poiInfoList.get(position).location.longitude);
                        } catch (URISyntaxException e) {
                            e.printStackTrace();
                        }
                    }else {
                        Snackbar.make(v,"未安装百度地图,跳转应用商店.", Snackbar.LENGTH_LONG).show();
                        Uri uri = Uri.parse("market://details?id=com.baidu.BaiduMap");// id为包名
                        Intent it = new Intent(Intent.ACTION_VIEW, uri);
                        mContext.startActivity(it);
                    }
                }
            });
        }

        @Override
        public int getItemCount() {
            return poiInfoList.size();
        }

        protected static class PhotoViewHolder extends RecyclerView.ViewHolder {
            CardView cardView;
            TextView textView_address;
            TextView distance;
            TextView textView_name;
//            ImageView imageViewDetail;
            public PhotoViewHolder(View view) {
                super(view);
                textView_address = (TextView) view.findViewById(R.id.textView_address);
                distance = (TextView) view.findViewById(R.id.distance);
                textView_name = (TextView) view.findViewById(R.id.textView_name);
//                imageViewDetail = (ImageView)view.findViewById(R.id.imageview_detail);
                cardView = (CardView)view.findViewById(R.id.poi_card_item_layout);
            }
        }

    public static boolean isAvilible(Context context, String packageName) {
        final PackageManager packageManager = context.getPackageManager();//获取packagemanager
        List<PackageInfo> pinfo = packageManager.getInstalledPackages(0);//获取所有已安装程序的包信息
        List<String> pName = new ArrayList<String>();//用于存储所有已安装程序的包名
        //从pinfo中将包名字逐一取出，压入pName list中
        if (pinfo != null) {
            for (int i = 0; i < pinfo.size(); i++) {
                String pn = pinfo.get(i).packageName;
                pName.add(pn);
            }
        }
        return pName.contains(packageName);//判断pName中是否有目标程序的包名，有TRUE，没有FALSE
    }

}
